package com.hwadee.job.controller;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.hwadee.job.entity.Admin;
import com.hwadee.job.entity.StudentInfo;
import com.hwadee.job.entity.Recruit;
import com.hwadee.job.entity.Company;

import com.hwadee.job.service.AdminService;


@RequestMapping("/admin")
@Controller
public class AdminController {
	
	@Autowired
	private AdminService ad;
	
	//访问后台登录页面
	@GetMapping("/")
	public String login() {
		return "admin/login";
	}
	
	//后台登录功能
	@PostMapping("login")
	public String index(Admin admin,Model model,HttpSession session) {
		String Zh=admin.getAdminZh();
		String psd=admin.getAdminPassword();
		
		if(Zh.equals(null)||Zh=="") {
			model.addAttribute("msg", "账号不能为空！");
			return "admin/login";
		}
		
		if(psd.equals(null)||psd=="") {
			model.addAttribute("psd", "密码不能为空！");
			return "admin/login";
		}
		
		Admin admin1=ad.selectByZh(Zh);
		
		if(admin1==null||!psd.equals(admin1.getAdminPassword())) {
			model.addAttribute("mg", "账户或密码错误！");
			return "admin/login";
		}else {
			session.setAttribute("Admin", admin1);
			return "redirect:/admin/index";
		}
		
		
	}
	
	//访问后台首页，未登录请先登录
	@GetMapping("index")
	public String shouye(Model model,HttpSession session) {
		if(session.getAttribute("Admin")==null) {
			JOptionPane.showMessageDialog(null, "请先登录！","提示",JOptionPane.ERROR_MESSAGE);
			return "redirect:/admin/";
		}else {
			return "admin/index";
		}
		
	}
	
	//退出首页，清空session
	@GetMapping("out")
	public String out(HttpSession session) {
		int res =JOptionPane.showConfirmDialog(null,"确认退出！","我要提示你",JOptionPane.YES_NO_OPTION);
		if(res == JOptionPane.YES_OPTION) {
			session.invalidate();
			return "redirect:/admin/";
		}else {
			return "redirect:/admin/index";
		}
		
	}
	
	//访问毕业生信息管理
	@GetMapping("page")
	public String stu(HttpSession session,Model model) {
		if(session.getAttribute("Admin")==null) {
			JOptionPane.showMessageDialog(null, "请先登录！","提示",JOptionPane.ERROR_MESSAGE);
			return "redirect:/admin/";
		}else {
			List<StudentInfo> list=ad.select();
			model.addAttribute("stu", list);
			return "admin/stumanamement";
		}
		
	}
	//分页
	@GetMapping("stu")
	public String findAll(@RequestParam(name="page",required = true,defaultValue = "1")int page,@RequestParam(name="size",required=true,defaultValue = "3") int size,Model model,HttpSession session) {
		if(session.getAttribute("Admin")==null) {
			JOptionPane.showMessageDialog(null, "请先登录！","提示",JOptionPane.ERROR_MESSAGE);
			return "redirect:/admin/";
		}else {
			List<StudentInfo> stulist=ad.findAll(page, size);
			PageInfo pageinfo=new PageInfo(stulist);
			model.addAttribute("stupage", pageinfo);
			return "admin/stumanamement";
		}
	}
	
	
	//根据学号搜索毕业生
	@PostMapping("sousuo")
	public String sousuo(String stuXh,Model model) {
		StudentInfo stu=ad.selectBysIdnumber(stuXh);
		model.addAttribute("stu", stu);
		return "admin/sousuo";
	}
	
	//访问添加毕业生信息
	@GetMapping("addstu")
	public String addstu(HttpSession session) {
		if(session.getAttribute("Admin")==null) {
			JOptionPane.showMessageDialog(null, "请先登录！","提示",JOptionPane.ERROR_MESSAGE);
			return "redirect:/admin/";
		}else {
		return "admin/addstu";
		}
	}
	//添加毕业生信息
	@PostMapping("addstu")
	public String stuadd(StudentInfo stu,Model model) {
		int a=ad.insertstu(stu);
		if(a==1) {
			JOptionPane.showMessageDialog(null, "添加成功！","提示",JOptionPane.ERROR_MESSAGE);
			return "redirect:/admin/stu";
		}else {
			JOptionPane.showMessageDialog(null, "添加失败！","提示",JOptionPane.ERROR_MESSAGE);
			return "admin/addstu";
		}
	}
	
	//删除毕业生信息
	@GetMapping("delete")
	public String deletestu(int id,Model model) {
		int res =JOptionPane.showConfirmDialog(null,"确认删除该毕业生吗！","我要提示你",JOptionPane.YES_NO_OPTION);
		if(res == JOptionPane.YES_OPTION) {
			ad.deleteByPrimaryKey(id);
			return "redirect:/admin/stu";
		}else {
			return "redirect:/admin/stu";
			}
		
	}
	
	//查看毕业生详情信息
	@GetMapping("details")
	public String xq(int id,Model model) {
		StudentInfo stu=ad.selectByKey(id);
		model.addAttribute("stu", stu);
		return "admin/xqstu";
	}
	
	//访问招聘信息管理首页
	@GetMapping("Recru")
	public String Recruit(HttpSession session,Model model) {
		if(session.getAttribute("Admin")==null) {
			JOptionPane.showMessageDialog(null, "请先登录！","提示",JOptionPane.ERROR_MESSAGE);
			return "redirect:/admin/";
		}else {
			List<Recruit> list=ad.selectRecruit();
			model.addAttribute("recruit", list);
			return "admin/Recruit";
		}
	}
	
	//分页
		@GetMapping("Recruit")
		public String findAllRecruit(@RequestParam(name="page",required = true,defaultValue = "1")int page,@RequestParam(name="size",required=true,defaultValue = "3") int size,Model model,HttpSession session) {
			if(session.getAttribute("Admin")==null) {
				JOptionPane.showMessageDialog(null, "请先登录！","提示",JOptionPane.ERROR_MESSAGE);
				return "redirect:/admin/";
			}else {
				List<Recruit> Recruitlist=ad.findAllRecruit(page, size);
				PageInfo pageinfo=new PageInfo(Recruitlist);
				model.addAttribute("recruit", pageinfo);
				return "admin/Recruit";
			}
		}
	
	
	
	//添加招聘信息页面
	@GetMapping("addrecruit")
	public String addrecruit(HttpSession session) {
		if(session.getAttribute("Admin")==null) {
			JOptionPane.showMessageDialog(null, "请先登录！","提示",JOptionPane.ERROR_MESSAGE);
			return "redirect:/admin/";
		}else {
			return "admin/addrecruit";
			}
		
	}
	//发布招聘信息
	@PostMapping("addrecruit")
	public String addre(Recruit rec) {
		int a=ad.inserrecruit(rec);
		if(a==1) {
			JOptionPane.showMessageDialog(null, "添加成功！","提示",JOptionPane.ERROR_MESSAGE);
			return "redirect:/admin/Recruit";
		}else {
			JOptionPane.showMessageDialog(null, "添加失败！","提示",JOptionPane.ERROR_MESSAGE);
			return "admin/addrecruit";
		}
		
	}
	
	//删除招聘信息
	@GetMapping("deleteByPrimaryKey")
	public String deleteByPrimaryKey(int id) {
		int res =JOptionPane.showConfirmDialog(null,"确认删除招聘信息吗！","我要提示你",JOptionPane.YES_NO_OPTION);
		if(res == JOptionPane.YES_OPTION) {
			ad.deleteByrecruitKey(id);
			return "redirect:/admin/Recruit";
		}else {
			return "redirect:/admin/Recruit";
			}
	}
	
	//招聘信息详情页面
	@GetMapping("xqrecruit")
	public String xqrecruit(HttpSession session,int id,Model model) {
		if(session.getAttribute("Admin")==null) {
			JOptionPane.showMessageDialog(null, "请先登录！","提示",JOptionPane.ERROR_MESSAGE);
			return "redirect:/admin/";
		}else {
			Recruit recruit=ad.selectByRecruitkey(id);
			model.addAttribute("recruit",recruit);
			return "admin/xqrecruit";
			}
		
	}
	
	//企业管理模块
	@GetMapping("Company")
	public String Company(@RequestParam(name="page",required = true,defaultValue = "1")int page,@RequestParam(name="size",required=true,defaultValue = "3") int size,Model model,HttpSession session) {
		if(session.getAttribute("Admin")==null) {
			JOptionPane.showMessageDialog(null, "请先登录！","提示",JOptionPane.ERROR_MESSAGE);
			return "redirect:/admin/";
		}else {
			List<Company> comlist=ad.findAllCompany(page, size);
			PageInfo pageinfo=new PageInfo(comlist);
			model.addAttribute("company", pageinfo);
			return "admin/Company";
		}
	}
	
	//删除企业
	@GetMapping("deleteCompany")
	public String deleteCompany(int id) {
		int res =JOptionPane.showConfirmDialog(null,"确认删除Company吗！","我要提示你",JOptionPane.YES_NO_OPTION);
		if(res == JOptionPane.YES_OPTION) {
			ad.deleteBycompanyKey(id);
			return "redirect:/admin/Company";
		}else {
			return "redirect:/admin/Company";
			}
	}

}
